﻿using System;
using System.Collections.Generic;
using System.Linq;
using Domain.Entity;

namespace EmailMarketingDao
{
   public class SmtpServerDao
    {
       public static List<SMTPServer> GetEmailSmtpServcer()
       {
           using (var db = new DBDataContext())
           {
               return db.SMTPServer.ToList();
           }
       }
       public static SMTPServer GetEmailSmtpServcerByName(string name)
       {
           using (var db = new DBDataContext())
           {
               return db.SMTPServer.FirstOrDefault(x=>x.Name==name);
           }
       }
       public static SMTPServer GetEmailSmtpServcerBySmtp(string smtp)
       {
           using (var db = new DBDataContext())
           {
               return db.SMTPServer.FirstOrDefault(x => x.Server == smtp);
           }
       }
       /// <summary>
       /// 更新代理邮局已经发送的数量
       /// </summary>
       /// <param name="smtp"></param>
       /// <param name="count"></param>
       public static void UpdateSMTPHasendCount(string smtp, int count)
       {
           using (var db = new DBDataContext())
           {
               var acount = db.SMTPServer.FirstOrDefault(x => x.Server == smtp);
               if (acount != null) acount.HasSendCount = count;
               db.SubmitChanges();
           }
       }
       /// <summary>
       /// 重置上次更新已发送量的时间
       /// </summary>
       /// <param name="smtp"></param>
       /// <param name="time"></param>
       public static void UpdateLastResetSendCountTime(string smtp, DateTime time)
       {
           using (var db = new DBDataContext())
           {
               var acount = db.SMTPServer.FirstOrDefault(x => x.Server == smtp);
               if (acount != null) acount.LastResetSendCountTime = time;
               db.SubmitChanges();
           }
       }
    }
}
